Service Methods Operation/Flows
1. setPreference(dto: CreateNotificationPreferenceDto): Promise of NotificationPreference
Purpose:
Create or update a notification preference for a customer.
Flow:
- Start
- Find Customer by
dto.customerId- If not found → throw
NotFoundException("Customer not found")
- If not found → throw
- Find NotificationService by
dto.notificationServiceId- If not found → throw
NotFoundException("Notification service not found")
- If not found → throw
- Check for existing NotificationPreference by:
- Customer ID
- NotificationService ID
- NotificationType
- If found:
- Update
statusandreason - Save and return updated NotificationPreference
- Update
- Else:
- Create new NotificationPreference
- Save and return new NotificationPreference
2. createNotificationService(createDto: CreateNotificationServiceDto, userId: number): Promise of NotificationService
Purpose:
Create a new notification service with a unique name, by an admin user.
Flow:
- Start
- Check if NotificationService with
name.toLowerCase()exists- If yes → throw
ConflictException("Notification service with name ... already exists")
- If yes → throw
- Find Customer by
userIdwithaccountTypeId = ADMIN- If not found → throw
NotFoundException("Admin record not found")
- If not found → throw
- Create new NotificationService entity with:
- name: lowercased
- createdBy: admin customer
- Save and return new NotificationService
3. updateNotificationService(id: number, dto: CreateNotificationServiceDto, userId: number): Promise of NotificationService
Purpose:
Update an existing notification service name by an admin, preventing duplicates.
Flow:
- Start
- Find NotificationService by
id- If not found → throw
NotFoundException("Notification service not found")
- If not found → throw
- Check if another NotificationService with
dto.name.toLowerCase()exists and has different id- If yes → throw
ConflictException("Another notification service with name ... already exists")
- If yes → throw
- Find Customer by
userIdwithaccountTypeId = ADMIN- If not found → throw
NotFoundException("Admin record not found")
- If not found → throw
- Update:
service.name = dto.name.toLowerCase()service.updatedBy = admin customer
- Save and return updated NotificationService
4. getNotificationService(): Promise of NotificationService array
Purpose:
Retrieve all notification services with createdBy and updatedBy info.
Flow:
- Start
- Fetch all NotificationService entities with relations:
createdBy(id, firstName, lastName)updatedBy(id, firstName, lastName)
- If none found → throw
NotFoundException("No notification services found") - Return list of notification services
5. getEmailNotificationOptOutDetails(token: string): Promise of an object with customer, notificationType, and service
Purpose:
Decode encrypted token to retrieve customer and service details for opt-out.
Flow:
- Start
- Decrypt token with secret key → get object with
serviceId,customerId, andnotificationType - Find NotificationService by
serviceId- If not found → throw
NotFoundException("Notification Service not found")
- If not found → throw
- Find Customer by
customerId- If not found → throw
NotFoundException("Customer not found")
- If not found → throw
- Return object containing
customer,notificationType, andservice
6. createOptOutLink(serviceId: number, customerId: number, notificationType: NotificationType): Promise of an object with link string
Purpose:
Generate a secure opt-out URL for customers to disable notifications.
Flow:
- Start
- Find NotificationService by
serviceId- If not found → throw
NotFoundException("Service with id ... not found")
- If not found → throw
- Encrypt payload with
serviceId,customerId, andnotificationTypeusing secret key - Construct URL:
${webAppDomain}/notification-preferences/opt-out/{encoded token} - Shorten
link - Return object containing the shortened
link
Notes
- All methods validate existence of critical entities (
Customer,NotificationService,Adminusers). - Conflict checks prevent duplicate service names.
- Encryption/decryption used for secure opt-out link generation.
- Exceptions are thrown clearly to indicate errors.